package day04.concurrent01;

import java.util.concurrent.CountDownLatch;

/**
 * 3.CountDownLatch 闭锁
 */
public class Demo04 {
    public static void main(String[] args) throws InterruptedException {
        //创建闭锁，指定内部计数数值
        CountDownLatch cdl = new CountDownLatch(3);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("小刘去买米..");
                    cdl.countDown();//闭锁内部的计数数值减一
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("大刘去买菜..");
                    cdl.countDown();//闭锁内部的计数数值减一
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(4000);
                    System.out.println("老刘去买锅..");
                    cdl.countDown();//闭锁内部的计数数值减一
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();


        cdl.await();//会进入阻塞，直到内部的保存的计数数值变为0
        System.out.println("刘妈妈做饭..");
    }
}
